package com.linktco.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

public class RegularUtils {

	public static void main(String[] args) {
		String strTemplate ="${111}${22}";
		String rgex = "\\$\\{(.*?)}";
		Matcher matcher = Pattern.compile(rgex).matcher(strTemplate);
		while (matcher.find()) {
			//获取a标签中的内容
			System.out.println(matcher.group(1));
			//获取a标签的内容(包含a标签)
			System.out.println(matcher.group());
		}
	}


	/**
	 * 是否包含中文
	 *
	 * @param str
	 * @return
	 */
	public static boolean isContainChinese(String str) {
		String reg = "[\u4e00-\u9fa5]";
		Pattern compile = Pattern.compile(reg);
		Matcher m = compile.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}


	/**
	 * 统计中文字符
	 *
	 * @param text
	 * @return
	 */
	public static int countChinese(String text) {
		int amount = 0;
		for (int i = 0; i < text.length(); i++) {
			boolean matches = Pattern.matches("^[\u4E00-\u9Fa5]{0,}$", text.charAt(i) + "");
			if (matches) {
				amount++;
			}
		}
		return amount;
	}



	public static boolean isPhone(String phone) {
		String[] patternArr = {"^1[3|4|5|6|7|8|9][0-9]{9}$","^[0][1-9]{2,3}-[0-9]{5,10}$","^[1-9]{1}[0-9]{5,8}$"};
		for (String s : patternArr) {
			Pattern p = compile(s);
			Matcher m = p.matcher(phone);
			if (m.matches()){
				return true;
			}
		}
		return false;
	}


}
